<template>
    <div class="search-result-list-container"  >
      <!-- 搜索结果页-头部导航 -->
      <div class="search-result-container">
        <!-- 点击实现后退效果 -->
        <van-nav-bar
          title="搜索结果"
          left-arrow
          @click-left="$router.go(-1)"
          fixed
        />
      </div>
      <!-- 搜索结果 -->
      <div class="searchList_content">
        <van-list
          v-model="loading"
          :finished='finished'
          @load="onLoadFn"
          offset="50"
          :immediate-check='false'
        >
          <article-item
          v-for="item in resultList"
          :key="item.art_id"
          :list='item'
          :isShowCross='false'
          ></article-item>
          </van-list>
      </div>
    </div>

</template>

<script>
import { getSearchResultListAPI } from '@/api/search'
let page = 1 // 页码
export default {
  name: 'SearchResultList',
  data () {
    return {
      resultList: [], // 搜索结果列表
      loading: false,
      finished: false
    }
  },
  async activated () {
    this.getSearchResultList()
  },
  // 组件切走或者失去激活是触发
  deactivated () {
    // 清除之前的数据，把页码制为一，每次还是从第一页开始请求
    page = 1
    this.resultList = []
  },
  computed: {
    keyword () {
      // 计算属性带缓存
      return this.$route.params.keyword
    }
  },
  methods: {
    // 获取搜索结果列表
    async  getSearchResultList () {
      const res = await getSearchResultListAPI({
      // this.$route.params.keyword 获取动态参数的值
        keyword: this.keyword,
        page: page++
      })
      const { results } = res.data
      if (results.length < 10) {
        this.finished = true
        return
      }
      // 合并数组
      this.resultList = [...this.resultList, ...res.data.results]
      // 可以继续下一页
      this.loading = false
    },
    onLoadFn () {
      // 上拉加载更多
      if (this.resultList.length > 0) {
        this.getSearchResultList()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
